package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.NotesReviewAndCustomerVO;
import com.chixing.pojo.TravelNotes;
import com.chixing.service.CustomerService;
import com.chixing.service.IndexService;
import com.chixing.service.TravelNotesService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private TravelNotesService travelNotesService;

    @GetMapping("index/{notesId}")
    @ResponseBody
    public ServerResponse getNotesById(@PathVariable("notesId") Integer notesId){
        ServerResponse response = indexService.getNotesById(notesId);
        return response;
    }

    @GetMapping(value = {"index/customer/{custId}","index/customer/{custId}/page/{pageNum}"})
    @ResponseBody
    public ServerResponse getNotesAll(@PathVariable(value = "pageNum",required = false) Integer pageNum,@PathVariable("custId") Integer custId){
        if(pageNum==null){
            pageNum=1;
        }
        return indexService.getNotesByPage(pageNum,custId);

    }

    @GetMapping(value = {"index/date","index/date/page/{pageNum}"})
    @ResponseBody
    public ServerResponse getNotesAllOrderByDate(@PathVariable(value = "pageNum",required = false) Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        return indexService.getNotesByPageOrderByDate(pageNum);
    }

//    @GetMapping("index/praise/count/{id}")
//    @ResponseBody
//    public ServerResponse updatePraiseAdd(@PathVariable("id") Integer id){
//        System.out.println(1111111111);
//        return indexService.updatePriseAdd(id);
//    }

    //点赞
    //http://localhost:8080/zyx/cusId/{cusId}/travelNotesId/{travelNotesId}/status/{status}
    @GetMapping("cusId/{cusId}/travelNotesId/{travelNotesId}/status/{status}")
    @ResponseBody
    public String praise(@PathVariable("cusId") Integer cusId,@PathVariable("travelNotesId") Integer travelNotesId,@PathVariable("status") Integer status){
        indexService.praise(cusId,travelNotesId,status);
        System.out.println(cusId);
        System.out.println(travelNotesId);
        System.out.println(status);
        return "ok";
    }


    @GetMapping("index/notes/details/{id}/{custId}")
    public ModelAndView getNotesDetailsById(@PathVariable("id")Integer id,@PathVariable("custId")Integer custId){
        ModelAndView mav = new ModelAndView();
        ServerResponse response = indexService.getNotesDetailById(id);
        TravelNotes data = (TravelNotes) response.getData();
        Integer travelNotesId = data.getTravelNotesId();
        List<NotesReviewAndCustomerVO> NRCvo = travelNotesService.getByNotesId(travelNotesId);
        mav.addObject("listVo",NRCvo);
        byte[] travelNotesText = data.getTravelNotesText();
        String content = new String(travelNotesText);
        mav.addObject("response",response);
        mav.addObject("text",content);
        ServerResponse customerServiceById = customerService.getById(custId);
        Customer customer = (Customer) customerServiceById.getData();
        mav.addObject("cust",customer);
        mav.setViewName("notes/travel_notes");
        return mav;
    }
}
